﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using NewsPortal.DataContracts;
using NewsPortal.Linq;
using NewsPortal.Models;

namespace NewsPortal.Controllers
{
    public class NewsController : Controller
    {
        //
        // GET: /News/
        [OutputCache(CacheProfile = "MyNewsArticleCache", VaryByParam = "id")]
        public ActionResult Run(string id)
        {
            var newsModel = new PortalModel();
            var newsid = new Guid(id);
            using (var connection = DataContextFactory.Create())
            {
                var newses =
                    from news in connection.GetTable<News>()
                    where news.Id == newsid
                    select news;
                var article = newses.Take(1).ToArray()[0];
                // GetCategoryList
                var categories =
                    from category in connection.GetTable<Category>()
                    orderby category.Priority
                    select category;
                newsModel.AddCategores(categories.ToList());
                // GetCategoryName
                var curCategory = categories.Where(x => x.Id == article.CategoryId).ToArray()[0];                    

                newsModel.AddNews(curCategory, newses.ToArray());
            }
            return View("NewsView",newsModel);
        }
        public ActionResult Index()
        {
            return RedirectToRoute("Default");
        }

    }
}
